﻿using System.Collections.Generic;
using Infrastructure.Coding;
using LinQProject;
using LinQProject.Repository;


namespace ServiceProject.EntityService
{
    public class UserService:DataBaseProcessBase<User>
    {
        public UserService(FarmacieDataClassesDataContext database) : base(database){}

        public bool IsUserAvailable(string userName, bool? isActive)
        {
            IList<User> listaUsers = 
                isActive == null ? base.GetAll(u => u.UserName == userName) 
                                 : base.GetAll(u => u.UserName == userName && u.IsActive == isActive);
            if (listaUsers.Count > 0)
               return false;
            return true;
        }

        public object InsertUser(User user)
        {
            return base.Add(user, "UserId");
        }

        public User GetUserByUserName(string userName)
        {
            IList<User> listUser = base.GetAll(u => u.UserName == userName && u.IsActive);
            if (listUser.Count > 0)
                return listUser[0];
            return null;
        }

        public bool IsCorectUserAndPassword(string userName, string password)
        {
            User userToFind = GetUserByUserName(userName);
            if (userToFind == null)
                return false;
            string encrytedPassword = new Encryption().encryptPassword(password);
            return userToFind.UserPassword == encrytedPassword;
           
        }
    }
}
